﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.DAL;
using System.Collections.Generic;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class ES_TrainingRequestApp : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        InitialData_BLL iniData = new InitialData_BLL();
        IJobSiteBLL jBLL = new JobSiteBLL();
        JobSiteInfo info = new JobSiteInfo();
        ConvertNullable cn = new ConvertNullable();
        TrainingCoursesInfo courseInfo = new TrainingCoursesInfo();
        TrainingCoursesDAL courseDAL = new TrainingCoursesDAL();
        ITrainingCoursesQuotaBLL qBLL = new TrainingCoursesQuotaBLL();
        ITrainingCoursesTrainerBLL tBLL = new TrainingCoursesTrainerBLL();
        ITrainingLecturerSkillsBLL lSkillBLL = new TrainingLecturerSkillsBLL();
        IEmployeeBLL EmpBLL = new EmployeeBLL();
        TrainingCoursesDesDtlsBLL CDesBLL = new TrainingCoursesDesDtlsBLL();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //lblID.Text = Request.QueryString["id"];
                //Session["id"] = Request.QueryString["id"];
                LoadDetail();               
                LoadDataGrid2();
               ;

            }

        }

        protected void LoadDetail()
        {
            int train_seq = Convert.ToInt32(Convert.ToInt32(Request.QueryString["Sequence"].ToString()));
            TrainingCoursesInfo info = courseDAL.GetData(train_seq);
            lbTrainingCourseDescr.Text = info.training_course_descr;
            //lbTotalScore.Text = info.total_score.ToString();
            lbTotalPrice.Text = info.total_price.ToString();
            if (info.date_sign_start.ToString().Length > 0)
            { lbDateSignStard.Text = info.date_sign_start.Value.ToShortDateString(); }
            if (info.date_sign_end.ToString().Length > 0)
            { lbDateSignEnd.Text = info.date_sign_end.Value.ToShortDateString(); }
            if (info.date_training_start.ToString().Length > 0)
            { lbDateTrainStard.Text = info.date_training_start.Value.ToShortDateString(); }
            if (info.date_training_end.ToString().Length > 0)
            { lbDateTrainEnd.Text = info.date_training_end.Value.ToShortDateString(); }
            lbTotalPrice.Text = info.total_price.ToString();
            //lbTotalScore.Text = info.total_score.ToString();
            lbMaxTrainee.Text = info.max_trainee_count.ToString();
            lbSchedule.Text = info.schedule_text.ToString();
            string TrainSeq = Request.QueryString["Sequence"].ToString();
            DataSet ListDataCommand = CDesBLL.GetData("", TrainSeq, "");
            if (ListDataCommand.Tables.Count > 0)
            {
                DataTable table = ListDataCommand.Tables[0];
                if (table.Rows.Count > 0)
                { lbQuota.Text = table.Rows[0]["COUNT_QUTA"].ToString(); }
            }
        }
        //======================================== รายชื่อผู้สมัคร  =================================================  
        private void LoadDataGrid2()
        {
            string TrainSeq = Request.QueryString["Sequence"].ToString();
            DataSet ListSalaryCommand = CDesBLL.GetData("", TrainSeq, "");
            DataTable table = ListSalaryCommand.Tables[0];
            lbTotalTrainee.Text = ListSalaryCommand.Tables[0].Rows.Count.ToString();
            RadGrid2.DataSource = ListSalaryCommand.Tables[0];
            RadGrid2.DataBind();
        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32(RadGrid2.Items[e.Item.ItemIndex]["trainee_seq"].Text);
                CDesBLL.DeleteTrainee(seq, ssMng.EmpSeq);
            }
            else if (e.CommandName == "AddData")
            {
                UpdateUI2("AddNew");
            }
            
            LoadDataGrid2();
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }
        
       
        protected void btnSave2_Click(object sender, EventArgs e)
        {
            if (checkValidate() == true)
            {
                //string strJobSite = cbJobSite.SelectedValue;//CDesBLL.GetData(cbEmployeeList.SelectedValue);
                //string strJobSite = ListJobSite.Tables[0];
                string TrainSeq = Request.QueryString["Sequence"].ToString();
                string User = ssMng.EmpSeq;
                string JobSiteSeq = "";
                string WorkingName = "";
                string ClassName = "";
                string PostName = "";
                DataSet ListSalaryCommand = EmpBLL.LoadEmployees(cn.ConvertNullInt(User), "1", true, "");
                if (ListSalaryCommand.Tables.Count > 0)
                {
                    DataTable table = ListSalaryCommand.Tables[0];
                    JobSiteSeq = table.Rows[0]["JOB_SITE_SEQ"].ToString();
                    WorkingName = table.Rows[0]["WORKING_JOB_SITE_NAME"].ToString();
                    ClassName = table.Rows[0]["CLASS_NAME"].ToString();
                    PostName = table.Rows[0]["POS_NAME"].ToString();
                }



                CDesBLL.InsertByTrainee(TrainSeq, User, "", JobSiteSeq, WorkingName, PostName, ClassName, User);
                LoadDataGrid2();
                //LoadcbTrainee();
                //UpdateUI2("Save");
            }
        }
        protected void btnCancel2_Click(object sender, EventArgs e)
        {
            UpdateUI2("Cancel");
            ClearControl();
        }
        protected void ClearControl()
        {
        }
        protected bool checkValidate()
        {
            bool bOK = true;
            //string strJobSite = cbJobSite.SelectedValue;
            //string strQuota = txtquota.Text;

            //if (strJobSite.Length == 0)
            //{
            //    bOK = false;
            //    lblError2.Text = "* กรุณาเลือกหน่วยงาน";
            //}
            return bOK;
        }
        protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    //PanelGrid2.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "AddNew":
                    //PanelGrid2.Visible = false;
                    PanelAddData2.Visible = true;                    
                    btnSave2.Visible = true;

                    break;

                case "Cancel":
                    //PanelGrid2.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "Update":
                   //PanelGrid2.Visible = false;
                    PanelAddData2.Visible = true;
                    btnSave2.Visible = false;                   

                    break;
            }
        }
    }
}
